package dao;

import domain.Equipment;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.DruidUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 简介说明: 设备数据访问层实现类
 *
 * @author: Half_Q_0115
 * @date: 2024/09/17 20:56:35
 * @version: 1.0
 */

public class EquipmentDaoImpl {
    private QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());

    public List<Equipment> findAllEquipment() {
        String sql = "select * from equipment";
        List<Equipment> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<Equipment>(Equipment.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public List<Equipment> findMyEquipment(String staff_id) {
        String sql = "SELECT * FROM equipment WHERE staff_id=?;";
        Object[] params = {staff_id};
        List<Equipment> list = null;
        try {
            list = qr.query(sql, new BeanListHandler<Equipment>(Equipment.class), params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public Equipment findEquipmentById(int id) {
        String sql = "select * from equipment where id = ?";
        Equipment equipment = null;
        try {
            equipment = qr.query(sql, new BeanHandler<>(Equipment.class), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return equipment;
    }

    public List<Equipment> searchEquipment(String id, String name, String type, String college_id, String staff_id) {
        StringBuilder sql = new StringBuilder("SELECT * FROM equipment WHERE 1=1");
        List<Object> params = new ArrayList<>();

        if (id != null && !id.trim().isEmpty()) {
            sql.append(" AND id = ?");
            params.add(id);
        }
        if (name != null && !name.trim().isEmpty()) {
            sql.append(" AND name LIKE ?");
            params.add("%" + name + "%");
        }
        if (type != null && !type.trim().isEmpty()) {
            sql.append(" AND type = ?");
            params.add(type);
        }
        if (college_id != null && !college_id.trim().isEmpty()) {
            sql.append(" AND college_id = ?");
            params.add(college_id);
        }
        if (staff_id != null && !staff_id.trim().isEmpty()) {
            sql.append(" AND staff_id = ?");
            params.add(staff_id);
        }

        try {
            return qr.query(sql.toString(), new BeanListHandler<>(Equipment.class), params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public int addEquipment(Equipment equipment) {
        String sql = "INSERT INTO equipment(name, type, college_id, staff_id, is_available, `status`, remarks) VALUES (?,?,?,?,?,?,?);";
        equipment.setStatus("空闲");
        Object[] params = new Object[]{equipment.getName(), equipment.getType(), equipment.getCollege_id(),
                equipment.getStaff_id(),equipment.getIs_available(), equipment.getStatus(), equipment.getRemarks()};
        int result = 0;
        try {
            result = qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
    public int modifyEquipment(Equipment equipment) {
        String sql = "UPDATE equipment SET name = ?, type = ?, is_available = ?, remarks = ? WHERE id = ?";
        Object[] params = {equipment.getName(), equipment.getType(), equipment.getIs_available(),
        equipment.getRemarks(), equipment.getId()};
        int result = 0;
        try {
            result = qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    public int deleteEquipmentById(String id)  {
        String sql = "DELETE FROM equipment WHERE id = ?";
        Object[] params = {id};
        int result = 0;
        try {
            result = qr.update(sql, params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

}
